<?php


namespace App\Http\Controllers\Back;


use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PageTemplateController extends Controller
{
    /**
     * 添加模板
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function add_template(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|unique:personal_page_template',
            'name' => 'required|unique:personal_page_template'
        ]);

        $data = [
            'id' => $request->input('id'),
            'name' => $request->input('name'),
            'cate' => $request->input('cate'),
            'cover' => $request->has('cover')?$request->input('cover'):'',
            'path' => $request->has('path')?$request->input('path'):'',
            'clazz' => $request->has('clazz')?$request->input('clazz'):'',
            'type' => $request->has('type')?$request->input('type'):'',
            'sort' => $request->has('sort')?$request->input('sort'):0,
            'describe' => $request->has('describe')?$request->input('describe'):'',
            'is_enabled' => 1,
            'content' => $request->has('content')?$request->input('content'):'',
            'created_at' => Carbon::now()
        ];
        DB::table('personal_page_template')->insert($data);
        return $this->success();
    }

    public function edit_template(Request $request, $id)
    {
        $this->validate($request, [
            'id' => 'unique:personal_page_template',
            'name' => 'unique:personal_page_template'
        ]);
        $data = [];
        if ($request->has('id')){
            $data['id'] = $request->input('id');
        }
        if ($request->has('name')){
            $data['name'] = $request->input('name');
        }
        if ($request->has('cate')){
            $data['cate'] = $request->input('cate');
        }
        if ($request->has('path')){
            $data['path'] = $request->input('path');
        }
        if ($request->has('type')){
            $data['type'] = $request->input('type');
        }
        if ($request->has('sort')){
            $data['sort'] = $request->input('sort');
        }
        if ($request->has('describe')){
            $data['describe'] = $request->input('describe');
        }
        if ($request->has('is_enabled')){
            $data['is_enabled'] = $request->input('is_enabled');
        }
        if ($request->has('content')){
            $data['content'] = $request->input('content');
        }
        if ($request->has('clazz')){
            $data['clazz'] = $request->input('clazz');
        }
        $data['updated_at'] = Carbon::now();
        DB::table('personal_page_template')->where('id',$id)->update($data);
        return $this->success();
    }

    public function del_template($id)
    {
        $temp = DB::table('personal_page_template')->find($id);
        if (empty($temp)) {
            throw new ModelNotFoundException();
        }
        DB::table('personal_page_template')->delete($id);
        return $this->success();
    }

    public function add_category(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|unique:personal_page_template_category'
        ]);

        $data = [
            'title' => $request->input('title'),
            'cover' => $request->has('cover')?$request->input('cover'):'',
            'is_enabled' => 1,
            'created_at' => Carbon::now()
        ];

        DB::table('personal_page_template_category')->insert($data);
        return $this->success();
    }

    public function add_custom_category(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|unique:personal_page_custom_categories'
        ]);
        
        $data = [
            'name' => $request->input('name'),
            'cover' => $request->has('cover')?$request->input('cover'):'',
            'class_name' => $request->has('class_name')?$request->input('class_name'):'',
            'type' => $request->has('type')?$request->input('type'):'',
            'is_enabled' => 1,
            'created_at' => Carbon::now()
        ];
        
        DB::table('personal_page_custom_categories')->insert($data);
        return $this->success();
    }

    public function add_custom_component(Request $request)
    {

        $data = [
            'name' => $request->input('name'),
            'cate' => $request->input('cate'),
            'cover' => $request->has('cover')?$request->input('cover'):'',
            'describe' => $request->has('describe')?$request->input('describe'):'',
            'has_vip' => $request->has('has_vip')?$request->input('has_vip'):'',
            'price' => $request->has('price')?$request->input('price'):'',
            'is_enabled' => 1,
            'content' => $request->has('content')?$request->input('content'):'',
            'ratio' => $request->has('ratio')?$request->input('ratio'):'',
            'type' => $request->has('type')?$request->input('type'):'',
            'is_slide' => $request->has('is_slide')?$request->input('is_slide'):'',
            'created_at' => Carbon::now()
        ];
        DB::table('personal_page_custom_component')->insert($data);
        return $this->success();
    }

    public function edit_custom_component(Request $request, $id)
    {
        $data = [];
        if ($request->has('name')) {
            $data['name'] = $request->input('name');
        }
        if ($request->has('cate')) {
            $data['cate'] = $request->input('cate');
        }
        if ($request->has('cover')) {
            $data['cover'] = $request->input('cover');
        }
        if ($request->has('describe')) {
            $data['describe'] = $request->input('describe');
        }
        if ($request->has('has_vip')) {
            $data['has_vip'] = $request->input('has_vip');
        }
        if ($request->has('price')) {
            $data['price'] = $request->input('price');
        }
        if ($request->has('content')) {
            $data['content'] = $request->input('content');
        }
        if ($request->has('ratio')) {
            $data['ratio'] = $request->input('ratio');
        }
        if ($request->has('is_slide')) {
            $data['is_slide'] = $request->input('is_slide');
        }
        $data['updated_at'] = Carbon::now();
        DB::table('personal_page_custom_component')->where('id',$id)->update($data);
        return $this->success();
    }

    public function del_custom_component($id)
    {
        $temp = DB::table('personal_page_custom_component')->find($id);
        if (empty($temp)) {
            throw new ModelNotFoundException();
        }
        DB::table('personal_page_custom_component')->delete($id);
        return $this->success();
    }
}